import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { Router } from '@angular/router';
import { HttpParams } from '@angular/common/http';
import { debug } from 'util';
import { NzMessageService } from 'ng-zorro-antd';
import { ServerResult } from '../../../infrastructure/server.result';
import { ServerPager } from '../../../infrastructure/server.pager';
import { VideoModel } from '../../../modes/video.model';
import { plainToClass } from 'class-transformer';
import { APPHOST } from  "../../../shared/index"
@Component({
  selector: 'app-cms-video',
  templateUrl: './video.component.html',
  styleUrls: ['./video.component.less'],
})
export class CmsVideoComponent implements OnInit {
  keywords: string = "";
  pageIndex = 1;
  pageSize = 10;
  total = 1;
  listOfData = [];
  loading = true;
  loadUrl = `${APPHOST}/api/video/list`;
  delUrl = `${APPHOST}/api/video/del`;
  previewVisible = false;
  videoUrl = "";

  constructor(private http: _HttpClient, private modal: ModalHelper, private router: Router, private msg: NzMessageService) { }

  ngOnInit() {
    this.searchData();
  }



  searchData(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;
    console.log(this.pageIndex);
    const pars = {
      keywords: this.keywords,
      pageIndex: this.pageIndex,
      pageSize: this.pageSize
    };
    this.http.get(this.loadUrl, pars).subscribe((data: any) => {
      const result = plainToClass(ServerResult, data) as ServerResult<ServerPager<VideoModel>>;
      if (!result.isOK()) {
        this.msg.create("error", result.errorMsg);
      } else {
        this.loading = false;
        this.total = result.data.recordCount;
        this.listOfData = result.data.list;
      }

    },
      err => {
        this.msg.create("error", "网络请求错误");
      });
  }

  add() {
    this.router.navigate(["cms/edit"]);
  }

  handleSearch(e) {
    this.searchData(true);
  }


  handlePreview(videoUrl: string) {
    this.videoUrl = videoUrl;
   // this.previewVisible = true;
    //https://github.com/giuseppe-dandrea/Stream-to-VLC
    window.location.href = "vlcs:" + this.videoUrl;
  }

  deleteItem(videoId: number) {
    let params = {
      videoId: videoId
    };
    this.http.get(this.delUrl, params).subscribe((data: any) => {
      const result = plainToClass(ServerResult, data) as ServerResult<ServerPager<VideoModel>>;
      if (!result.isOK()) {
        this.msg.create("error", result.errorMsg);
      } else {
        this.searchData();
      }

    },
      err => {
        this.msg.create("error", "网络请求错误");
      });

  }
}
